﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace JEdwonTune.Lib.Hibernate {
    public class DbUtil {

        public static String ParseDataType2ProertyType(String dataType,Boolean rNullAble) {
            if (String.IsNullOrEmpty(dataType)) return "undefined";
            dataType = dataType.ToLower();
            if (DataTypeDict != null && DataTypeDict.ContainsKey(dataType)) {
                String propertyType = DataTypeDict[dataType];
                String pType = propertyType.ToLower();
                if (pType == "decimal" 
                    || pType == "datetime" 
                    || pType =="long" 
                    || pType =="short" 
                    || pType =="float" 
                    || pType=="double" 
                    || pType=="int" 
                    || pType == "Int32" 
                    || pType == "Int64" 
                    || pType == "Int16"
                    )
                    return propertyType + "?";
                else
                    return propertyType;
            } else {
                return "UnknownT";
            }
        }

        public static Dictionary<String, String> DataTypeDict;

        public static void ParseDataType() {
            DataTypeDict = new Dictionary<string, string>();
            XmlDocument doc = new XmlDocument();
            doc.Load("Hibernate/TypeMapping.xml");
            XmlNodeList nl = doc.SelectNodes("//sql-type");
            foreach (XmlNode n in nl) {
                DataTypeDict.Add(n.Attributes["dbtype"].Value, n.Attributes["nhtype"].Value);
            }
        }
    
    }
}
